package com.wlx.common.message.sms;

import com.wlx.common.core.utils.MapUtil;
import com.wlx.common.core.utils.http.HttpUtil;
import com.wlx.common.message.util.scheduler.SchedulerUtil;
import com.wlx.common.message.Sender;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Admin
 */
public class RongTongDaSmsSender implements Sender {

    @Override
    public boolean send(String receiver, String message, String title, Map<String, Object> senderParameter) throws Exception {
        Map<String, String> parameterMap = new HashMap<>(2);
        parameterMap.put("pn", receiver);
        parameterMap.put("msg", title + message);
        parameterMap.put("account", senderParameter.get("account") + "");
        parameterMap.put("pswd", senderParameter.get("pswd") + "");
        Object host = senderParameter.get("rHost");
        Object api = senderParameter.get("api");
        HttpUtil.HttpResponse response = null;
        try {
            response = HttpUtil.connection(host.toString() + api.toString()).setParameter(parameterMap).post();
        } catch (Exception e) {
            throw e;
        }
        return true;
    }

    @Override
    public boolean syncSend(String receiver, String message, String title, Map<String, Object> senderParameter) throws Exception {
        SchedulerUtil.schedule(() -> {
            send(receiver, message, title, senderParameter);
        });
        return true;
    }

    @Override
    public Map<String, Object> senderParameterTemplate() {
        return MapUtil.builder().elem("account", "账户，由蓉通达后台获取，类似：113913")
                .elem("pswd", "密码")
                .elem("host", "主机地址：由蓉通达提供，类似：http://122.144.203.27:8001")
                .elem("api", "api地址：由蓉通达提供，类似：/mt.ashx").build();
    }
}
